import monsterHandle from './monsterHandle';
import {
    totalConfig,
    propName
} from 'server/config/templateConfig';
import equipmentHandle from './equipmentHandle';
import fightAttri from 'server/model/fightAttri';
import playerHandle from './playerHandle';
import partnerHandle from './partnerHandle';
// let montsersConfig = totalConfig.monster;

class BattleHandle {
    static getInstance() {
        if (!BattleHandle.instance) {
            BattleHandle.instance = new BattleHandle();
        }
        return BattleHandle.instance;
    }
    test(player) {
        let p1 = [],
            p2 = [];
        let monsterId = 320001;
        let battleTeams = player.battleTeams;
        for (let j = 0; j < 3; j++) {
            let partner = battleTeams[j];
            if (partner) {
                p1.push(partnerHandle.getFightAttriCopy(partner));
            } else {
                p1.push(null);
            }
        }
        let monster = monsterHandle.createMonster(monsterId);
        p2.push(monsterHandle.getFightAttriCopy(monster));
        p2.push(monsterHandle.getFightAttriCopy(monster));
        p2.push(monsterHandle.getFightAttriCopy(monster));

        let battleMsg = {
            p1: p1,
            p2: p2,
            result: 0 //1:队伍1胜利 2:队伍2胜利
        };
        return battleMsg;
    }
    battle(player, monsterId) {
        let p1 = [],
            p2 = [];
        let fightAttriTeam1 = [],
            fightAttriTeam2 = [],
            battleQueue = [],
            battleTeams = player.battleTeams,
            team1 = {
                team: fightAttriTeam1
            },
            team2 = {
                team: fightAttriTeam2
            };
        for (let j = 0; j < 3; j++) {
            let partner = battleTeams[j];
            if (partner) {
                p1.push(partnerHandle.getFightAttriCopy(partner));
                let fightAttri = partnerHandle.getFightAttri(partner);
                fightAttri.teamId = 1;
                fightAttri.myTeam = team1;
                fightAttri.enemyTeam = team2;
                fightAttriTeam1.push(fightAttri);
                battleQueue.push(fightAttri);
            } else {
                p1.push(null);
            }
        }
        team1.aliveNumber = fightAttriTeam1.length;
        let monsters = monsterHandle.createMonsters(monsterId);
        for (let j = 0; j < 3; j++) {
            let monster = monsters[j];
            if (monster) {
                p2.push(partnerHandle.getFightAttriCopy(monster));
                let fightAttri = monsterHandle.getFightAttri(monster);
                fightAttri.teamId = 2;
                fightAttri.myTeam = team2;
                fightAttri.enemyTeam = team1;
                fightAttriTeam2.push(fightAttri);
                battleQueue.push(fightAttri);
            } else {
                p2.push(null);
            }
        }
        team2.aliveNumber = fightAttriTeam2.length;
        let battleMsg = {
            p1: p1,
            p2: p2,
            result: 0 //1:队伍1胜利 2:队伍2胜利
        };

        let length = battleQueue.length;
        let round = 1;
        let flag = true;
        while (flag) {
            for (let i = 0; i < length; i++) {
                if (battleQueue[i].alive !== false) {
                    let attacker = battleQueue[i];
                    let shield = 1;
                    let enemyTeam = attacker.enemyTeam.team;
                    for (let k = 0; k < enemyTeam.length; k++) {
                        let defender = enemyTeam[k];
                        if (defender.alive !== false) {
                            this.calcDamage(attacker, defender);
                            if (defender.hp <= 0) {
                                defender.alive = false;
                                attacker.enemyTeam.aliveNumber -= 1;
                                if (attacker.enemyTeam.aliveNumber === 0) {
                                    flag = false;
                                    battleMsg.result = attacker.teamId;
                                    break;
                                }
                            }
                            if (defender.currentShield != 0) {
                                let value = defender.currentShield - shield;
                                if (value < 0) {
                                    defender.currentShield = 0;
                                    shield = value * -1;
                                } else {
                                    defender.currentShield = value;
                                    shield = 0;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            round += 1;
            if (round % 2 == 1) {
                battleQueue.forEach(item => {
                    if (item.alive !== false) {
                        item.currentShield = item.shield;
                        item.hp += item.recover;
                        if (item.hp > item.maxHp) {
                            item.hp = item.maxHp
                        }
                    }
                });
            }
        }
        return battleMsg;
    }

    //计算攻击
    calcDamage(attacker, defender) {
        let pdef = defender.pdef,
            attack = attacker.attack;
        let damage = Math.ceil(attack * attack / (pdef + attack)); //计算伤害
        defender.hp -= damage;
        // console.log(`attacker:${attacker.name}|hp:${attacker.hp};defender:${defender.name}|hp:${defender.hp};伤害:${damage}`)
        return damage;
    }

}

let battleHandle = BattleHandle.getInstance();
export default battleHandle;